package cn.jilian.sys.web;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.jilian.common.domain.R;
import cn.jilian.common.dto.PageResult;
import cn.jilian.common.util.MapUtil;
import cn.jilian.entity.Unit;
import cn.jilian.entity.User;
import cn.jilian.security.util.ShiroUtils;
import cn.jilian.sys.domain.UnitListQuery;
import cn.jilian.sys.service.UnitService;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/unit")
public class UnitController {
	
	@Autowired
	private UnitService unitService;
	@Autowired
	private ObjectMapper objectMapper;
	
	@ApiOperation("所有机构管理列表")
	@RequiresPermissions("system:units:view")
	@GetMapping("/orgs.html")
	public ModelAndView mgrView(HttpSession session) {
		ModelAndView mv=new ModelAndView("gui/tab");
		mv.addObject("jsfile", "sys.org_mgr");
		mv.addObject("title", "机构组织管理");
		return mv;
	}
	
	@ApiOperation("登录用户所属机构信息维护")
	@RequiresPermissions("system.unit.info")
	@GetMapping("/unit_info.html")
	public ModelAndView userUnitView() {
		User user=ShiroUtils.getUser();
		Unit unit=unitService.findBySid(user.getUnitId());
		ModelAndView mv=new ModelAndView("settings/unit_info");
		mv.addObject("unit", unit);
		return mv;
	}
	
	@ApiOperation("特定类型机构管理列表")
	@GetMapping("/list.html")
	public ModelAndView unitMgrView(@RequestParam("metaId")Long metaId,HttpSession session) {
		ModelAndView mv=new ModelAndView();
		return mv;
	}
	
	@GetMapping("/{id}")
	@ResponseBody
	public Object unitTree(@PathVariable("id")Long id) {
		return unitService.findByFid(id);
	}
	
	@PutMapping("")
	public R addUnit(@RequestParam Map<String,Object> data) {
		try {
			data=MapUtil.expand(data);
			Long metaId=Long.parseLong((String) data.get("meta"));
			Unit unit=objectMapper.convertValue(data, Unit.METACLASSMAP.get(metaId));
			unitService.add(unit);
			return R.ok("新增成功");
		}catch(Exception e) {
			e.printStackTrace();
			return R.fail("新增失败");
		}
	}
	@PostMapping("")
	public R updateUnit(@RequestParam Map<String,Object> data) {
		try {
			data=MapUtil.expand(data);
			Long metaId=Long.parseLong((String) data.get("meta"));
			Unit unit=objectMapper.convertValue(data, Unit.METACLASSMAP.get(metaId));
			unitService.save(unit);
			return R.ok("更新成功");
		}catch(Exception e) {
			e.printStackTrace();
			return R.fail("更新失败");
		}
	}
	
	@DeleteMapping("/{id}")
	public R deleteUnit(@PathVariable("id")Long id) {
		try {
			unitService.delete(id);
			return R.ok;
		}catch(Exception e) {
			e.printStackTrace();
			return R.fail("删除失败");
		}
	}
	
	@ApiOperation("特定类型机构管理列表")
	@RequiresPermissions("system:units:page")
	@PostMapping("/list")
	public PageResult<Unit> queryUnits(UnitListQuery query) {
		
		return unitService.query(query.getPageInfo(), query.getSorInfo(), query.getFilters());
	}
	
}
